//
//  InspectionFormModel.swift
//  Employee
//
//  Created by swordjoy on 2017/4/5.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import Foundation
import SwiftyJSON

struct InspectionFormModel {
  
  let id: String
  let number: String
  let areaName: String
  let region: String
  let itemName: String
  let formItems: [FormItem]
  let status: String
  
  init(json: JSON) {
    self.id = json["id"].stringValue
    self.number = json["number"].stringValue
    self.areaName = json["areaName"].stringValue
    self.region = json["region"].stringValue
    self.itemName = json["itemName"].stringValue
    self.formItems = FormItem.array(from: json["widgets"].arrayValue)
    self.status = json["status"].stringValue
  }
  
}

struct FormItem: ModelProtocol {
  
  let name: String
  let type: String
  let required: Bool
  let options: [Option]
  
  init(json: JSON) {
    
    self.name = json["name"].stringValue
    self.type = json["type"].stringValue
    self.options = Option.array(from: json["options"].arrayValue)
    self.required = json["required"].boolValue
    
  }
  
  static func array(from jsonArray: [JSON]) -> [FormItem] {
    return jsonArray.map({
      return FormItem(json: $0)
    })
  }
  
}

public struct Option: ModelProtocol {
  
  let name: String
  
  public init(json: JSON) {
    self.name = json["name"].stringValue
  }
  
  public static func array(from jsonArray: [JSON]) -> [Option] {
    return jsonArray.map({
      return Option(json: $0)
    })
  }
}
